# -*- coding: UTF-8 -*-
'''
Get figures(*.jpg, maybe *.eps *.png and so on) from tecplot file(*.lpk or *.lay)
This script is used only Windows.
Input : tecplot files and their name list  
Output: figures 

生成list文件：
dir /b > list
'''

import subprocess,os

#tecFileExt = 'lpk'
#figFileExt = 'jpg'
#tecFileExt = 'lpk'
figFileExt = 'png'
#Never add '\' in the end because this will be used in tecplot macro.
tecFileDir = r'D:\tecplot文件目录'
figFileDir = r'D:\输出图片文件目录'

#tecplot 2010
#tecEXE = r"F:\Program Files\Tecplot\Tec360 2010\bin\tec360.exe"
#tecplot 2017
tecEXE = r"C:\Program Files\Tecplot\Tecplot 360 EX 2017 R2\bin\tec360.exe"

listFile=os.path.join(tecFileDir,'list')
tecFileOpen = open(listFile,'r')
for tecFile in tecFileOpen:	
	sourceFileName = tecFile.strip()
	targetFileName = sourceFileName.split('.')[0]+'.'+figFileExt
	print('Generating %-30s......' % targetFileName,end='')
	#check generate which figure
	if (figFileExt == 'jpg'):
		#generate *.mcr file
		tecMcrOpen = open('genFigTmp.mcr','w')
		tecMcrOpen.write('#!MC 1200\n')
		tecMcrOpen.write('# Created by Tecplot 360 build 12.2.0.9077\n')
		tecMcrOpen.write("$!VarSet |sourceDir| = '"+tecFileDir+"'\n")
		tecMcrOpen.write("$!VarSet |targetDir| = '"+figFileDir+"'\n")
		tecMcrOpen.write("$!VarSet |sourceFile| = '"+sourceFileName+"'\n")
		tecMcrOpen.write("$!VarSet |targetFile| = '"+targetFileName+"'\n")
		tecMcrOpen.write('$!OPENLAYOUT  "|sourceDir|\|sourceFile|"\n')
		tecMcrOpen.write('$!EXPORTSETUP EXPORTFORMAT = JPEG\n')
		tecMcrOpen.write('$!EXPORTSETUP IMAGEWIDTH = 1024\n')
		tecMcrOpen.write("$!EXPORTSETUP EXPORTFNAME = '|targetDir|\|targetFile|'\n")
		tecMcrOpen.write('$!EXPORTSETUP EXPORTREGION = ALLFRAMES\n')
		tecMcrOpen.write('$!EXPORT \n')
		tecMcrOpen.write('  EXPORTREGION = ALLFRAMES\n')
		tecMcrOpen.write('$!Quit\n')
		tecMcrOpen.write('$!RemoveVar |sourceDir|\n')
		tecMcrOpen.write('$!RemoveVar |targetDir|\n')
		tecMcrOpen.write('$!RemoveVar |sourceFile|\n')
		tecMcrOpen.write('$!RemoveVar |targetFile|\n')
		tecMcrOpen.close()
		#run mcr file
		tecOutEXE = subprocess.check_output([tecEXE,'-p','genFigTmp.mcr'])
		print('Done')
	elif (figFileExt == 'png'):
		#generate *.mcr file
		tecMcrOpen = open('genFigTmp.mcr','w')
		tecMcrOpen.write('#!MC 1200\n')
		tecMcrOpen.write('# Created by Tecplot 360 build 12.2.0.9077\n')
		tecMcrOpen.write("$!VarSet |sourceDir| = '"+tecFileDir+"'\n")
		tecMcrOpen.write("$!VarSet |targetDir| = '"+figFileDir+"'\n")
		tecMcrOpen.write("$!VarSet |sourceFile| = '"+sourceFileName+"'\n")
		tecMcrOpen.write("$!VarSet |targetFile| = '"+targetFileName+"'\n")
		tecMcrOpen.write('$!OPENLAYOUT  "|sourceDir|\|sourceFile|"\n')
		tecMcrOpen.write('$!EXPORTSETUP EXPORTFORMAT = PNG\n')
		tecMcrOpen.write('$!EXPORTSETUP IMAGEWIDTH = 1024\n')
		tecMcrOpen.write("$!EXPORTSETUP EXPORTFNAME = '|targetDir|\|targetFile|'\n")
		tecMcrOpen.write('$!EXPORTSETUP EXPORTREGION = ALLFRAMES\n')
		tecMcrOpen.write('$!EXPORT \n')
		tecMcrOpen.write('  EXPORTREGION = ALLFRAMES\n')
		tecMcrOpen.write('$!Quit\n')
		tecMcrOpen.write('$!RemoveVar |sourceDir|\n')
		tecMcrOpen.write('$!RemoveVar |targetDir|\n')
		tecMcrOpen.write('$!RemoveVar |sourceFile|\n')
		tecMcrOpen.write('$!RemoveVar |targetFile|\n')
		tecMcrOpen.close()
		#run mcr file
		tecOutEXE = subprocess.check_output([tecEXE,'-p','genFigTmp.mcr'])
		print('done')      